
'use strict';
const qiniuUploadFile = require('../utils/qiniu').qiniuUploadFile;
const fs = require('mz/fs');
const Controller = require('egg').Controller;

class CommonController extends Controller {
  // 七牛上传图片
  async uploadImgToQiNiu() {
    const { ctx } = this;
    const result = {
      code: 0,
    };
    let status = 200;


    const file = ctx.request.files[0];
    // const fileName = file.filename;
    try {
      // console.log('filename:', fileName);
      // 设置七牛云相关代码
      const res = await qiniuUploadFile(file);
      // console.log('res:', res);
      result.data = res;
      status = 200;

      ctx.body = result;
      ctx.status = status;
      return;


    } catch (err) {
      ctx.logger.error('uploadImgToQiNiu err', err);
      status = 500;
    } finally {
      // eslint-disable-next-line
      await fs.unlink(file.filepath)
    }
    ctx.body = result;
    ctx.status = status;
  }
}

module.exports = CommonController;
